﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using zuoye.Models;
namespace zuoye.Controllers
{
    public class DefaultController : Controller
    {
        // GET: Default
        public ActionResult Index()
        {
            //1
            //Model1 model = new Model1();
            //var list = model.Task.FirstOrDefault(p => p.TaskID == 1);
            //2
            Model1 model = new Model1();
            var list = model.Task.ToList();
            //把整个List集合传到前台
            return View(list);
        }
        public ActionResult Detail(int te)
        {
            /*
             前台向后台传值
            1.Request.QueryString["TaskID"](必须是Task中的TaskID)数据类型转换
            2.参数化   可以指定数据类型
            3.实例化    包含参数属性的一个实例对象
             */
            Model1 mo = new Model1();
            var task = mo.Task.FirstOrDefault(p => p.TaskID == te);

            return View(task);
        }
        public ActionResult Gai(string TaskId) {
            Model1 mo = new Model1();
            
            return View();       
        }
        [HttpPost]
        public ActionResult Delete(int id)
        {
            Model1 mo = new Model1();
            var task = mo.Task.FirstOrDefault(p => p.TaskID == id);
            if (task != null) {
                mo.Task.Remove(task);
                var returns = mo.SaveChanges() > 0;
                return Json(returns);
            }
            return Json(false);
        }
        public ActionResult Add1()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Add1(Task task)
        {
            //将默认值给添加与给空值赋值

            //实例化
            Model1 mo = new Model1();
            //添加到数据库
            mo.Task.Add(task);
            //保存到数据库
            var res = mo.SaveChanges();
            //判断
            if (res > 0)
                return RedirectToAction("Index");
            
            return View();
        }
       
        
        public ActionResult BianJi(int te)
        {
            Model1 mo = new Model1();
            var task = mo.Task.FirstOrDefault(p => p.TaskID == te);
            return View(task);
        }
        [HttpPost]
        public ActionResult BianJi(Task task)
        {
            Model1 mo = new Model1();
            mo.Entry(task).State = System.Data.Entity.EntityState.Modified;
            var res = mo.SaveChanges();
            if (res > 0)
                return RedirectToAction("Index");

            return View();
        }
    }

}